/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author CEBALLOS
 */
public class BuscarIMG extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String connectionURL = "jdbc:mysql://localhost:3306/oracle";
        java.sql.Connection con;
        Statement st1;

        String cedula = request.getParameter("cedula").toUpperCase();

        System.out.println(".........." + cedula);
        try {

            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con = DriverManager.getConnection(connectionURL, "root", "city123");
            st1 = con.createStatement();

            ResultSet rs1 = st1.executeQuery("select foto from datos where cedula='" + cedula + "' ");

            String imgLen = "";

            while (rs1.next()) {

                imgLen = rs1.getString(1);

                System.out.println(imgLen.length());

                int len = imgLen.length();

                byte[] rb = new byte[len];

                InputStream readImg = rs1.getBinaryStream(1);

                int index = readImg.read(rb, 0, len);
                System.out.println("Byte;;;;;;;;;;;;" + index);
                System.out.println("**************" + readImg);

                response.reset();

                response.setContentType("image/jpg");

                response.getOutputStream().write(rb, 0, len);/*: devuelve un objeto tipo 
                 ServletOutputStream; empleando este objeto es posible construir Servlets que 
                 generen respuestas binarias (por ejemplo, una imagen) y no sólo texto. */


                response.getOutputStream().flush();

            }

            st1.close();
            response.getOutputStream().close();

        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException | IOException e) {
        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        processRequest(request, response);

    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
